<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-row>
        <el-col :span="6">
          <!-- <el-button class="filter-item" size="small" style="margin-left: 10px;" type="primary" v-waves icon="el-icon-edit" @click="updatePaymentClick">修改</el-button> -->
          <el-button class="filter-item" size="small" style="margin-left: 10px;" type="primary" v-waves icon="el-icon-delete" @click="deletePaymentClick">删除</el-button>
          <el-button class="filter-item" size="small" style="margin-left: 10px;" type="primary" v-waves icon="el-icon-delete" @click="auditPaymentClick">审核</el-button>
        </el-col>
        <el-col :span="18" style="text-align: right">
          <el-input size="small" style="width: 200px" class="filter-item fsc-search" placeholder="付款单编号" v-model="listQuery.condition.docCode" @keyup.enter.native="searchClick"></el-input>
          <el-button class="filter-item" size="small" type="primary" v-waves icon="el-icon-search" @click="searchClick">
            搜索
          </el-button>
          <el-button class="filter-item" size="small" type="primary" v-waves icon="el-icon-circle-plus-outline" @click="moreClick">更多
          </el-button>
          <el-dropdown trigger="click" :hide-on-click=false style="float: right;margin-left: 10px">
            <span class="el-dropdown-link">
              <el-button class="filter-item" size="small" type="primary" v-waves>
                选择列
                <i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-checkbox-group v-model="theadCheckVal">
                  <el-checkbox label="docTypeName">单据类型</el-checkbox>
                  <el-checkbox label="docCode">单据编号</el-checkbox>
                  <el-checkbox label="paymentDate">付款日期</el-checkbox>
                  <el-checkbox label="partnerName">供应商</el-checkbox>
                  <el-checkbox label="useWay">用途</el-checkbox>
                  <el-checkbox label="settlCompanyName">结算公司</el-checkbox>
                  <el-checkbox label="status">单据状态</el-checkbox>
                  <el-checkbox label="originalCurrencySum">金额</el-checkbox>
                  <el-checkbox label="sourceOrderCodeLine">来源单据号</el-checkbox>
                  <el-checkbox label="gmtCreate">创建日期</el-checkbox>
                  <el-checkbox label="remark">备注</el-checkbox>
                </el-checkbox-group>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
      <div style="clean:both"></div>
    </div>
    <div :class="{'search-transition':searchFlag,h0:true,h110:searchFlag,'search-content':true}">
      <el-row>

        <el-col :xl="5" :lg="7">
          <div class="el-form-item postInfo-container-item el-form-item--medium">
            <label class="el-form-item__label" style="width: 80px;">单据状态:</label>
            <div class="el-form-item__content" style="margin-left: 80px;">
              <div class="el-input el-input--medium">
                <el-select v-model="listQuery.condition.status" placeholder="请选择单据状态">
                  <el-option label="新增" value="0"></el-option>
                  <el-option label="已审核" value="1"></el-option>
                  <el-option label="已完成" value="3"></el-option>
                </el-select>
              </div>
            </div>
          </div>
        </el-col>

        <el-col :xl="5" :lg="7">
          <div class="el-form-item postInfo-container-item el-form-item--medium">
            <label class="el-form-item__label" style="width: 80px;">供应商:</label>
            <div class="el-form-item__content" style="margin-left: 80px;">
              <div class="el-input el-input--medium">
                <el-select v-model="listQuery.condition.partnerId" filterable placeholder="请选择供应商">
                  <el-option v-for="partner in partnerList" :key="partner.id" :label="partner.name" :value="partner.id"></el-option>
                </el-select>
              </div>
            </div>
          </div>
        </el-col>

        <el-col :xl="5" :lg="7">
          <div class="el-form-item postInfo-container-item el-form-item--medium">
            <label class="el-form-item__label" style="width: 80px;">结算公司:</label>
            <div class="el-form-item__content" style="margin-left: 80px;">
              <div class="el-input el-input--medium">
                <el-select v-model="listQuery.condition.settlCompanyId" placeholder="请选择结算公司">
                  <el-option v-for="company in companyList" :key="company.id" :label="company.name" :value="company.id"></el-option>
                </el-select>
              </div>
            </div>
          </div>
        </el-col>

        <el-col :xl="5" :lg="7">
          <div class="el-form-item postInfo-container-item el-form-item--medium">
            <label class="el-form-item__label" style="width: 80px;">单据类型:</label>
            <div class="el-form-item__content" style="margin-left: 80px;">
              <div class="el-input el-input--medium">
                <el-select v-model="listQuery.condition.docType" placeholder="请选择单据类型">
                  <el-option v-for="shouldPaymentType in shouldPaymentTypeList" :key="shouldPaymentType.id" :label="shouldPaymentType.name" :value="shouldPaymentType.id"></el-option>
                </el-select>
              </div>
            </div>
          </div>
        </el-col>

      </el-row>
    </div>
    <div style="clean:both"></div>

    <el-table ref="paymentTable" :data="paymentList" v-loading="listLoading" element-loading-text="加载中" border fit highlight-current-row style="width: 100%;margin-top:5px">
      <el-table-column type="selection" width="35"></el-table-column>
      <el-table-column :key="key" align="center" :width="value.width" v-for='(value,key) in tableThead' :label="value.title">
        <template slot-scope="scope">
          <div v-if="value.key === 'status'">{{scope.row[value.key] | paymentStatusFilter}}</div>
          <div v-else-if="value.key === 'useWay'">{{scope.row[value.key] | useWayFilter}}</div>
          <div v-else-if="value.key === 'sourceOrderCodeLine'">
            <el-tooltip v-if="scope.row[value.key]" class="item" effect="dark" :content="scope.row[value.key]" placement="top-start">
              <el-button>{{scope.row[value.key] | sourceOrderCodeFilter}}</el-button>
            </el-tooltip>
          </div>
          <div v-else>{{scope.row[value.key]}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="100" class-name="small-padding">
        <template slot-scope="scope">
          <el-button class="filter-item" size="small" style="margin-left: 10px;" type="primary" v-waves icon="el-icon-delete" @click="updatePaymentClick(scope.row.id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.current" :page-sizes="[10,20,30,50]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="listQuery.total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  listPayment,
  deletePayment,
  getPayment,
  auditPayment
} from '@/api/pay/payment'
import {
  getPartnerList,
  getCompanyList,
  getShouldPaymentTypeListByBusinessType
} from '@/api/baseDataCommon'
import waves from '@/directive/waves' // 水波纹指令
const defautlTableThead = [
  { key: 'docTypeName', title: '单据类型', width: 106 },
  { key: 'docCode', title: '单据编号', width: 160 },
  { key: 'paymentDate', title: '付款日期', width: 120 },
  { key: 'partnerName', title: '供应商', width: 220 },
  { key: 'useWay', title: '用途', width: 80 },
  { key: 'settlCompanyName', title: '结算公司', width: 220 },
  { key: 'status', title: '单据状态', width: 106 },
  { key: 'originalCurrencySum', title: '金额', width: 106 },
  { key: 'sourceOrderCodeLine', title: '来源单据号', width: 170 },
  { key: 'gmtCreate', title: '创建日期', width: 106 },
  { key: 'remark', title: '备注', width: '', fixed: 'right' }
]
export default {
  name: 'payment',
  directives: {
    waves
  },
  data() {
    return {
      theadCheckVal: [
        'docTypeName',
        'docCode',
        'paymentDate',
        'partnerName',
        'useWay',
        'settlCompanyName',
        'status',
        'originalCurrencySum',
        'sourceOrderCodeLine',
        'gmtCreate',
        'remark'
      ],
      tableThead: defautlTableThead,
      search: {},
      listQuery: {
        current: 1,
        size: 10,
        total: 0,
        condition: {}
      },
      paymentList: [],
      listLoading: true,
      searchFlag: false,
      partnerList: [], // 供应商
      companyList: [], // 结算公司
      shouldPaymentTypeList: [] // 单据类型
    }
  },
  created() {
    // 获取供应商
    getPartnerList().then(result => {
      this.$data.partnerList = result.data
    })
    // 获取结算公司
    getCompanyList().then(result => {
      this.$data.companyList = result.data
    })
    // 应付单类型
    // TODO 业务类型 70 80 90
    getShouldPaymentTypeListByBusinessType(['70', '80', '90']).then(result => {
      this.$data.shouldPaymentTypeList = result.data
    })
    this.list()
  },
  watch: {
    theadCheckVal(valArr) {
      this.tableThead = defautlTableThead.filter(v => {
        for (var i = 0; i < valArr.length; i++) {
          if (v.key === valArr[i]) {
            return v
          }
        }
      })
    }
  },
  methods: {
    list() {
      // 列表查询
      this.$data.listLoading = true
      // 处理是否展开更多
      let queryParam = {}
      if (this.$data.searchFlag) {
        queryParam = this.$data.listQuery
      } else {
        Object.assign(queryParam, this.$data.listQuery)
        queryParam.condition = {
          docCode: this.$data.listQuery.condition.docCode
        }
      }
      listPayment(queryParam).then(result => {
        if (result.code === 0) {
          this.$data.paymentList = result.data.records
          this.$data.listQuery.total = result.data.total
        } else {
          this.$notify({
            title: '信息提示',
            message: result.message,
            type: 'error',
            duration: 2000
          })
        }
        this.$data.listLoading = false
      })
    },
    handleSizeChange(val) {
      // 分页size变化
      this.$data.listQuery.size = val
      this.list()
    },
    handleCurrentChange(val) {
      // 分页当前页变化
      this.$data.listQuery.current = val
      this.list()
    },
    addPaymentClick() {
      // 新增付款单
      this.$data.dialogFormVisible = true
    },
    moreClick() {
      // 更多click
      this.$data.searchFlag = !this.$data.searchFlag
    },
    searchClick() {
      // 搜索
      this.list()
    },
    deletePaymentClick() {
      // 删除付款单 click
      if (this.$refs.paymentTable.selection.length !== 0) {
        var ids = []
        for (var payment of this.$refs.paymentTable
          .selection) {
          ids.push(payment.id)
        }
        deletePayment(ids).then(result => {
          if (result.code === 0) {
            this.list() // 刷新列表
            this.$notify({
              title: '信息提示',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          } else {
            this.$notify({
              title: '信息提示',
              message: '请选择要删除的数据',
              type: 'error',
              duration: 2000
            })
          }
        })
      } else {
        this.$notify({
          title: '信息提示',
          message: '请选择要删除的数据',
          type: 'error',
          duration: 2000
        })
      }
    },
    updatePaymentClick(id) {
      // 更新付款单 click
      getPayment(id).then(result => {
        if (result.code === 0) {
          this.$router.push({
            path: 'paymentForm', name: '付款录入',
            params: { id: id, data: result.data }
          })
        } else {
          this.$notify({
            title: '信息提示',
            message: result.message,
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    auditPaymentClick() { // 审核click
      if (this.$refs.paymentTable.selection.length > 0) {
        var ids = []
        this.$confirm('是否审批选中数据?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          for (const key in this.$refs.paymentTable.selection) {
            ids.push(this.$refs.paymentTable.selection[key].id)
          }

          auditPayment(ids).then(result => {
            if (result.code === 0) {
              this.$notify({
                title: '信息提示',
                message: '审核付款单成功',
                type: 'success',
                duration: 2000
              })
              this.list()
            } else {
              this.$notify({
                title: '信息提示',
                message: result.message,
                type: 'error',
                duration: 2000
              })
            }
          })
        })
      } else {
        this.$notify({
          title: '信息提示',
          message: '请选择付款单数据',
          type: 'error',
          duration: 2000
        })
      }
    }
  }
}
</script>
<style scoped>

</style>
